#ifndef BEJ_UPDATE_H
#define BEJ_UPDATE_H
#include "test.h"
#include "down.h"


#include <iostream>
#include <random>
#include <ctime>
#include "test.h"
#include "down.h"
using namespace std;

struct Bejmatrix
{

    int arr[8][8];

    int boolarrx[8][8];

    int boolarry[8][8];

};

struct BejDevideMatrix
{
    int arr[8];
    int boolarr[8];
};

void BejRebuild(int arr[8][8],int boolarrx[8][8],int boolarry[8][8])
{
    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            if(boolarrx[i][j]==0 || boolarry[i][j]==0)
            {
                arr[i][j]=0;
            }
        }
    }
}

void update(int arr[8][8],int boolarrx[8][8],int boolarry[8][8])
{
    default_random_engine generator(time(0));
    uniform_int_distribution<int> distribution(1, 4);

    BejDevideMatrix x1;
    BejDevideMatrix x2;
    BejDevideMatrix x3;
    BejDevideMatrix x4;
    BejDevideMatrix x5;
    BejDevideMatrix x6;
    BejDevideMatrix x7;
    BejDevideMatrix x8;
    BejDevideMatrix y1;
    BejDevideMatrix y2;
    BejDevideMatrix y3;
    BejDevideMatrix y4;
    BejDevideMatrix y5;
    BejDevideMatrix y6;
    BejDevideMatrix y7;
    BejDevideMatrix y8;

    for(int i=0;i<8;i++)
    {
        x1.arr[i]=arr[0][i];
        x2.arr[i]=arr[1][i];
        x3.arr[i]=arr[2][i];
        x4.arr[i]=arr[3][i];
        x5.arr[i]=arr[4][i];
        x6.arr[i]=arr[5][i];
        x7.arr[i]=arr[6][i];
        x8.arr[i]=arr[7][i];
        y1.arr[i]=arr[i][0];
        y2.arr[i]=arr[i][1];
        y3.arr[i]=arr[i][2];
        y4.arr[i]=arr[i][3];
        y5.arr[i]=arr[i][4];
        y6.arr[i]=arr[i][5];
        y7.arr[i]=arr[i][6];
        y8.arr[i]=arr[i][7];
        x1.boolarr[i]=1;
        x2.boolarr[i]=1;
        x3.boolarr[i]=1;
        x4.boolarr[i]=1;
        x5.boolarr[i]=1;
        x6.boolarr[i]=1;
        x7.boolarr[i]=1;
        x8.boolarr[i]=1;
        y1.boolarr[i]=1;
        y2.boolarr[i]=1;
        y3.boolarr[i]=1;
        y4.boolarr[i]=1;
        y5.boolarr[i]=1;
        y6.boolarr[i]=1;
        y7.boolarr[i]=1;
        y8.boolarr[i]=1;
    }
    BejSolve(x1.arr,x1.boolarr);
    BejSolve(x2.arr,x2.boolarr);
    BejSolve(x3.arr,x3.boolarr);
    BejSolve(x4.arr,x4.boolarr);
    BejSolve(x5.arr,x5.boolarr);
    BejSolve(x6.arr,x6.boolarr);
    BejSolve(x7.arr,x7.boolarr);
    BejSolve(x8.arr,x8.boolarr);
    BejSolve(y1.arr,y1.boolarr);
    BejSolve(y2.arr,y2.boolarr);
    BejSolve(y3.arr,y3.boolarr);
    BejSolve(y4.arr,y4.boolarr);
    BejSolve(y5.arr,y5.boolarr);
    BejSolve(y6.arr,y6.boolarr);
    BejSolve(y7.arr,y7.boolarr);
    BejSolve(y8.arr,y8.boolarr);



    for(int i=0;i<8;i++)
    {
        boolarrx[0][i]=x1.boolarr[i];
        boolarrx[1][i]=x2.boolarr[i];
        boolarrx[2][i]=x3.boolarr[i];
        boolarrx[3][i]=x4.boolarr[i];
        boolarrx[4][i]=x5.boolarr[i];
        boolarrx[5][i]=x6.boolarr[i];
        boolarrx[6][i]=x7.boolarr[i];
        boolarrx[7][i]=x8.boolarr[i];
        boolarry[i][0]=y1.boolarr[i];
        boolarry[i][1]=y2.boolarr[i];
        boolarry[i][2]=y3.boolarr[i];
        boolarry[i][3]=y4.boolarr[i];
        boolarry[i][4]=y5.boolarr[i];
        boolarry[i][5]=y6.boolarr[i];
        boolarry[i][6]=y7.boolarr[i];
        boolarry[i][7]=y8.boolarr[i];
    }


    cout<<"After Devide and Solve:"<<endl;
    BejRebuild(arr,boolarrx,boolarry);
    int count=0;
    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            if(arr[i][j]==0)
            count++;
            cout<<arr[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<count;
    int down1[8];
    int down2[8];
    int down3[8];
    int down4[8];
    int down5[8];
    int down6[8];
    int down7[8];
    int down8[8];
    for(int i=0;i<8;i++)
    {
        down1[i]=arr[i][0];
        down2[i]=arr[i][1];
        down3[i]=arr[i][2];
        down4[i]=arr[i][3];
        down5[i]=arr[i][4];
        down6[i]=arr[i][5];
        down7[i]=arr[i][6];
        down8[i]=arr[i][7];
    }
    Down(down1);
    Down(down2);
    Down(down3);
    Down(down4);
    Down(down5);
    Down(down6);
    Down(down7);
    Down(down8);
    cout<<endl;
    cout<<"After Down:"<<endl;
    for(int i=0;i<8;i++)
    {
        arr[i][0]=down1[i];
        arr[i][1]=down2[i];
        arr[i][2]=down3[i];
        arr[i][3]=down4[i];
        arr[i][4]=down5[i];
        arr[i][5]=down6[i];
        arr[i][6]=down7[i];
        arr[i][7]=down8[i];
    }
    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            cout<<arr[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<"After Rebuild:"<<endl;
    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            if(arr[i][j]==0)
            arr[i][j]=distribution(generator);
            cout<<arr[i][j]<<" ";
            boolarrx[i][j]=1;
            boolarry[i][j]=1;
        }
        cout<<endl;
    }
    if(count>0)
    {
        update(arr,boolarrx,boolarry);
    }
}

#endif // BEJ_UPDATE_H